/*
 * @Date: 2023-03-27 16:35:00
 * @LastEditTime: 2023-06-22 12:23:02
 * 介绍:系统钩子
 */
import PubSub from "pubsub-js";
import type { StoreDefinition } from "pinia";
import { sys } from "@@/enum/pubsub";
/**
 * * 重置所有本地仓库
 * @param 不包括的store $id数组
 * @argument 默认不包括用户设置
 */
export function resetStore(exclude?: string[], excludeDefault = ["SysStore"]) {
  const sotres = Object.values(
    import.meta.glob("@@/store/**.ts", { eager: true })
  );
  console.log("重置所有store");
  sotres.forEach((val: any) => {
    const useStore = val.default as StoreDefinition;
    if (
      exclude?.includes(useStore.$id) ||
      excludeDefault.includes(useStore.$id)
    )
      return;
    const store = useStore();
    store.$reset();
  });
}

/**
 * * 系统装载成功
 * @param callback 回调
 * @argument 登录成功后调用，包括重新登录后
 */
export function sysLoad(callback: (a: string, v: any) => any) {
  let mySub: string;
  onMounted(() => {
    if (window.isLogined) {
      callback(sys.logined, undefined);
    }
    mySub = PubSub.subscribe(sys.logined, callback);
  });
  onUnmounted(() => {
    PubSub.unsubscribe(mySub);
  });
}